<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="基地" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--          <a-input v-decorator="['sourceBase']" placeholder="请输入基地"></a-input>-->
          <a-select  v-decorator="['sourceBase', validatorRules.sourceBase]" placeholder="请选择所属基地" @change="handleChange">
            <a-select-option v-for="d in baseList" :key="d.id" :value="d.id">{{ d.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="地块" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--          <a-input v-decorator="['sourceGreenhouse']" placeholder="请输入地块"></a-input>-->
          <a-select  v-decorator="['sourceGreenhouse', validatorRules.sourceGreenhouse]" placeholder="请选择所属大棚" >
            <a-select-option v-for="d in greenhouseList" :key="d.id"  :value="d.id">{{ d.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="采收产品" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--          <a-input v-decorator="['variety', validatorRules.variety]" placeholder="请输入采收产品"></a-input>-->
          <a-select  v-decorator="['variety', validatorRules.variety]" placeholder="请选择产品"   @change="varietyChange" >
            <a-select-option v-for="(d,i) in varietyList" :itemscope='d.code' :pack='d.packingSpecifications' :leng='d.flowerPoleLength' :key="d.id" :value="d.name">{{ d.name }}</a-select-option>
          </a-select>
          <a-input  v-decorator="['varietyCode', validatorRules.varietyCode]"  disabled='disabled'  ></a-input>
        </a-form-item>


        <a-form-item label="打包规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <a-input v-decorator="['packingSpecifications', validatorRules.packingSpecifications]" placeholder="请输入打包规格" disabled></a-input>
<!--          <a-select   v-decorator="['packingSpecifications', validatorRules.packingSpecifications]" placeholder="请选择打包规格"  >-->
<!--            <a-select-option v-for="(d,i) in psfList"  :key="d.id" :value="d.title">{{ d.title }}</a-select-option>-->
<!--          </a-select>-->
        </a-form-item>
        <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['flowerPoleLength', validatorRules.flowerPoleLength]" placeholder="请输入单位" disabled></a-input>
<!--          <a-select  v-decorator="['flowerPoleLength', validatorRules.flowerPoleLength]" placeholder="请选择单位"  >-->
<!--            <a-select-option v-for="(d,i) in lengthList"  :key="d.id" :value="d.title">{{ d.title }}</a-select-option>-->
<!--          </a-select>-->
        </a-form-item>
        <a-form-item label="入库数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['tagNum', validatorRules.tagNum]" placeholder="请输入入库数量" style="width: 100%"/>
        </a-form-item>

<!--        <a-form-item label="等级" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--          <a-select  v-decorator="['classes', validatorRules.classes]" placeholder="请选择等级"  >-->
<!--            <a-select-option v-for="(d,i) in classesList"  :key="d.id" :value="d.title">{{ d.title }}</a-select-option>-->
<!--          </a-select>-->
<!--        </a-form-item>-->
        <a-form-item label="分拣日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择分拣日期" v-decorator="['sortTime']" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="创建日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date  disabled='disabled'  placeholder="请选择创建日期" v-decorator="['createTime']" :trigger-change="true" style="width: 100%"/>
        </a-form-item>

        <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['state']" placeholder="请输入状态（0正常，1删除）" disabled='disabled'></a-input>
        </a-form-item>
        <a-form-item label="打印标签次数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number disabled='disabled' v-decorator="['printNum']" placeholder="请输入打印标签次数（0未打印，1打印，）" style="width: 100%"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import { queryFlowerBase, ajaxGetDictItems, queryGreenhouseByPd ,getVarietyList} from '@/api/api'

  export default {
    name: "FlowerTagInModal",
    components: {
      JDate,
    },
    data () {
      return {
        baseList: [], //基地信息
        greenhouseList: [], //大棚信息
        classesList: [], //等级信息
        varietyList: [], //品类信息
        psfList: [], //打包规格
        lengthList: [], //长度信息
        varietyCode: "",
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          // packingSpecifications: {
          //   rules: [
          //     { required: true, message: '请选择打包规格!'},
          //   ]
          // },
          sourceBase: {
            rules: [
              { required: true, message: '请选择基地!'},
            ]
          }, sourceGreenhouse: {
            rules: [
              { required: true, message: '请选择大棚!'},
            ]
          },/* classes: {
            rules: [
              { required: true, message: '请选择等级!'},
            ]
          },*/
          variety: {
            rules: [
              { required: true, message: '请选择采收产品!'},
            ]
          },
          varietyCode: {
            rules: [
              { required: true, message: '请选择采收产品!'},
            ]
          },
          packingSpecifications: {
            rules: [
              { required: true, message: '请选择打包规格!'},
            ]
          },
          tagNum: {
            rules: [
              { required: true, message: '请输入入库数量!'},
            ]
          },
          flowerPoleLength: {
            rules: [
              { required: true, message: '请选择单位!'},
            ]
          },
        },
        url: {
          add: "/flower/flowerTagIn/add",
          edit: "/flower/flowerTagIn/edit",
          dicUrl : "",
          getBasename: "/flower/flowerTagIn/edit",
        }
      }
    },
    created () {
      this.initBaseInfo()    //初始化基地信息
      this.initVarietyList()//初始化产品信息
      // this.initClassifyDict()//初始化等级信息
      // this.initpsfDict()//初始化打包规格
      // this.initlengthDict()//
    },
    methods: {
      varietyChange(value,itemscope) {
        // validatorRules
        if(itemscope && itemscope.data && itemscope.data.attrs && itemscope.data.attrs.itemscope){
         this.varietyCode = itemscope.data.attrs.itemscope
          this.form.setFieldsValue({
            varietyCode :  itemscope.data.attrs.itemscope
          })
        }else{
          this.varietyCode = undefined
          this.form.setFieldsValue({
            varietyCode :  undefined
          })
        }

        if(itemscope && itemscope.data && itemscope.data.attrs && itemscope.data.attrs.leng){

          this.form.setFieldsValue({
            flowerPoleLength :  itemscope.data.attrs.leng
          })
        }else{
          this.form.setFieldsValue({
            flowerPoleLength :  undefined
          })
        }

        if(itemscope && itemscope.data && itemscope.data.attrs && itemscope.data.attrs.pack){
          this.form.setFieldsValue({
            packingSpecifications :  itemscope.data.attrs.pack
          })
        }else{
          this.form.setFieldsValue({
            packingSpecifications :  undefined
          })
        }
      },
      handleChange(value) {
        queryGreenhouseByPd(value).then((res)=>{
          if(res.success){
            this.greenhouseList = res.result;
            this.form.setFieldsValue({sourceGreenhouse: undefined})
          }
        });
      },
      handleEditChange(value) {
        queryGreenhouseByPd(value).then((res)=>{
          if(res.success){
            this.greenhouseList = res.result;
          }
        });
      },
      //初始化基地信息
      initBaseInfo(){
        queryFlowerBase().then((res)=>{
          if(res.success){
            this.baseList = res.result;
          }
        });
      },
      //初始化产品信息
      initVarietyList(){
        getVarietyList().then((res)=>{
          if(res.success){
            this.varietyList = res.result;
          }
        });
      },
      //初始化等级信息
      initClassifyDict(){
        const dictCode = 'planting_flower_level'
        ajaxGetDictItems(dictCode, null).then((res) => {
          if (res.success) {
            this.classesList = res.result;
          }
        })
      },
      //初始化打包规格信息
      initpsfDict(){
        const dictCode = 'planting_packing_specifications'
        ajaxGetDictItems(dictCode, null).then((res) => {
          if (res.success) {
            this.psfList = res.result;
          }
        })
      }, //
      initlengthDict(){
        const dictCode = 'planting_flower_pole'
        ajaxGetDictItems(dictCode, null).then((res) => {
          if (res.success) {
            this.lengthList = res.result;
          }
        })
      },
      initVarietyDict(){
        const dictCode = 'planting_variety'
        ajaxGetDictItems(dictCode, null).then((res) => {
          if (res.success) {
            this.varietyList = res.result;
          }
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.handleEditChange(record.sourceBase)
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'sourceBase','sourceGreenhouse','variety','classes','varietyCode','sortTime','packingSpecifications','tagNum','flowerPoleLength','createTime','updateTime','createUser','updateUser','state','printNum'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            // console.log("表单提交数据",formData)
            // formData.varietyCode = this.varietyCode
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'sourceBase','sourceGreenhouse','variety','varietyCode','classes','sortTime','packingSpecifications','tagNum','flowerPoleLength','createTime','updateTime','createUser','updateUser','state','printNum'))
      },


    }
  }
</script>